package com.creative.accountsdk.holder;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.code.CodeCaptureActivity;
import com.creative.accountsdk.AccountSdk;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.OnLoginListener;
import com.creative.accountsdk.R;
import com.creative.accountsdk.UserData;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.ChildBindParentProtocol;
import com.creative.accountsdk.net.account.ParentBindChildProtocol;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * Created by wang_qian on 2015/11/24.
 */
public class BindAcountHolder extends BaseHolder {
    private boolean   parentBindChild = true;

    private TextView  tvInviteCodeHint;
    private TextView  tvCreateChildAccount;
    private Button    btnBind;
    private EditText  edtInviteCode;
    private  TextView tvScan2DCode;

    public BindAcountHolder(boolean  parentbindchild) {

        parentBindChild = parentbindchild;
    }

    @Override
    protected void initViews() {
        tvInviteCodeHint = (TextView)viewRoot.findViewById(R.id.tvInputInviteCodeBind);
        tvCreateChildAccount = (TextView)viewRoot.findViewById(R.id.tvCreateChildAccount);
        btnBind = (Button)viewRoot.findViewById(R.id.btnBind);
        edtInviteCode = (EditText)viewRoot.findViewById(R.id.EdtTxtInviteCode);
        tvScan2DCode = (TextView)viewRoot.findViewById(R.id.tv2dCodeBind);

        if (parentBindChild) {
            tvCreateChildAccount.setVisibility(View.VISIBLE);
            tvInviteCodeHint.setText(activity.getResources().getString(R.string.input_childinvitecode_bind));
        } else {
            tvInviteCodeHint.setText(activity.getResources().getString(R.string.input_parentinvitecode_bind));
        }

        btnBind.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(edtInviteCode.getText().toString())) {
                    String inviteCode = edtInviteCode.getText().toString();
                    progress = DialogUtils.createProgress(activity);
                    progress.show();

                    if (parentBindChild) {
                        final ParentBindChildProtocol protocol = new ParentBindChildProtocol(inviteCode);
                        protocol.debugUrl(DataCenter.getHttpClient());
                        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                super.onSuccess(statusCode, headers, response);
                                progress.cancel();
                                if (protocol.parse(response, activity)) {
                                    UserData.getInstance().initKids(protocol.kidsJsonArray);
                                    UserData.getInstance().writeKids(activity, protocol.kidsJsonArray);
                                    activity.finish();
                                }
                            }
                        });
                    } else {
                        final ChildBindParentProtocol protocol = new ChildBindParentProtocol(inviteCode);
                        protocol.debugUrl(DataCenter.getHttpClient());
                        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                super.onSuccess(statusCode, headers, response);
                                progress.cancel();
                                if (protocol.parse(response, activity)) {
                                    UserData.getInstance().initParent(protocol.parentJson);
                                    UserData.getInstance().writeParent(activity, protocol.parentJson);
                                    activity.finish();
                                }
                            }
                        });

                    }
                }
            }
        });
        tvScan2DCode.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                goScan2DCode();
            }
        });

        tvCreateChildAccount.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                goCreateChildAccount();
            }
        });

    }

    @Override
    public int getLayoutResID() {
        return R.layout.bind_account_layout;

    }


    private void goScan2DCode() {
        Intent intent = new Intent(activity, CodeCaptureActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivityForResult(intent, Constants.REQUEST_CODE_SCAN);

    }

    public void setInviteCodeFromScan(String scanresult) {
        edtInviteCode.setText(scanresult);
    }

    private void goCreateChildAccount() {
        activity.hideHolder();
        activity.showHolder(new RegisteHolder(true));
    }

}
